package com.ktjy.ktmall.servlet;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.ktjy.ktmall.entity.AddressParams;
import com.ktjy.ktmall.entity.AjaxResult;
import com.ktjy.ktmall.service.AddressService;
import com.ktjy.ktmall.service.impl.AddressServiceImpl;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.stream.Collectors;

// 用于处理收货地址相关的请求，比如新增收货地址
@WebServlet(name = "AddressServlet", urlPatterns = "/uni/member/address")
public class AddressServlet extends HttpServlet {
    private AddressService service = null;

    @Override
    public void init(ServletConfig config) throws ServletException {
        service = new AddressServiceImpl();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1、创建out对象
        PrintWriter out = response.getWriter();
        // 2、获取从小程序端请求过来的参数
        String params = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
        // 3、将请求参数字符串转换成JSONObject对象，之后从JSONObject对象中取出值
        JSONObject entries = JSONUtil.parseObj(params);
        String opr = entries.get("opr", String.class);
        AjaxResult result = null;
        if ("insert".equals(opr)) {
            // 3.1获取从小程序端请求过来的收货地址AddressParams对象信息
            AddressParams address = entries.get("address", AddressParams.class);
            // 3.2 调用service中的新增方法
            int i = service.insert(address);
            if (i > 0) {
                result = AjaxResult.success("新增成功!");
            } else {
                result = AjaxResult.error("新增失败!");
            }
        } else if ("findByUserId".equals(opr)) {
            // 获取从小程序端传递过来的userId用户id参数
            Integer userId = entries.get("userId", Integer.class);
            // 调用service中的findByUserId()实现通过用户id查询此用户的收货地址列表
            List<AddressParams> list = service.findByUserId(userId);
            result = AjaxResult.success("我的收货地址查询成功!", list);
        } else if ("findById".equals(opr)) {
            // 获取从小程序传递过来的地址id
            Integer id = Integer.valueOf(entries.get("id", Integer.class));
            // 2、调用service中的的findById()实现通过id查询地址详情
            AddressParams address = service.findById(id);
            result = AjaxResult.success("地址详情查询成功", address);
        } else if ("update".equals(opr)) {
            // 3.1获取从小程序端请求过来的收货地址AddressParams对象信息
            AddressParams address = entries.get("address", AddressParams.class);
            // 3.2 调用service中的新增方法
            int i = service.update(address);
            if (i > 0) {
                result = AjaxResult.success("修改成功!");
            } else {
                result = AjaxResult.error("修改失败!");
            }
        } else if ("del".equals(opr)) {
            // 获取从小程序传递过来的地址id
            Integer id = Integer.valueOf(entries.get("id", Integer.class));
            // 2、调用service中的的findById()实现通过id查询地址详情
            int i = service.delById(id);
            if (i > 0) {
                result = AjaxResult.success("删除成功!");
            } else {
                result = AjaxResult.error("删除失败!");
            }
        } else if ("getDefaultAdd".equals(opr)) {
            // 获取从小程序传递过来的用户id
            Integer userId = Integer.valueOf(entries.get("userId", Integer.class));
            // 2、调用service中根据登录用户id查询此用户默认的收货地址
            AddressParams address = service.getDefaultAddress(userId);
            result = AjaxResult.success("查询成功!", address);
        }
        out.print(JSON.toJSONString(result));
        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
